<?php
require_once(dirname(__FILE__)."/config.php");
require_once DEDEINC.'/membermodel.cls.php';
if($cfg_mb_allowreg=='N')
{
	ShowMsg('系统关闭了新用户注册！', 'index.php');
	exit();
}


if(!isset($dopost))
{
	$dopost = '';
}
$step = empty($step)? 1 : intval(preg_replace("/[^\d]/",'', $step));

if($step == 1)
{
	if($cfg_ml->IsLogin())
	{
		//if($cfg_mb_reginfo == 'Y')
		//{
			//如果启用注册详细信息
			//if($cfg_ml->fields['spacesta'] == 0 || $cfg_ml->fields['spacesta'] == 1)
			//{
				// ShowMsg("尚未完成详细资料，请完善...","index_do.php?fmdo=user&dopost=regnew&step=2",0,1000);
				// exit;
			//}
		//}
		ShowMsg('你已经登陆系统，无需重新注册！', 'index.php');
		exit();
	}
	if($dopost=='regbase')
	{
		$svali = GetCkVdValue();
		if(preg_match("/1/",$safe_gdopen)){
			if(strtolower($vdcode)!=$svali || $svali=='')
			{
				ResetVdValue();
				ShowMsg('验证码错误！', '-1');
				exit();
			}
			
		}
		if(!CheckEmail($email))
		{
			ShowMsg('Email格式不正确！', '-1');
			exit();
		}
		$mobile=isset($mobile) && is_numeric($mobile) ? $mobile : 0;
		if($mobile=='0' or $email=='')
		{
			ShowMsg('手机号码或邮箱必须添加！', '-1');
			exit();
		}
		
		
		
		//检测用户名是否存在
		$row = $dsql->GetOne("Select mid From `#@__member` where mb='$mobile' ");
		if(is_array($row))
		{
			ShowMsg("手机号码 {$mobile} 已注册，请重试！", "-1");
			exit();
		}
		
		//检测邮箱是否存在
		$row = $dsql->GetOne("Select mid From `#@__member` where email='$email' ");
		if(is_array($row))
		{
			ShowMsg("邮箱地址 {$mobile} 已注册，请重试！", "-1");
			exit();
		}
	
	
		$mobile = trim($mobile);
		$email = HtmlReplace($email,1);
		$passwd = trim($passwd);
	    $passwdag = trim($passwdag);
		
		if($passwd != $passwdag)
		{
			ShowMsg('你两次输入的密码不一致！', '-1');
			exit();
		}

	
		//会员的默认金币

		$jointime = time();
		$joinip = GetIP();
		
		$pwd = md5($passwd);

		
		
		
		
		
		
		$inQuery = "INSERT INTO `#@__member` (`pwd` ,`rank` ,`money` ,`email`,`jointime`, `joinip` ,`logintime`,`loginip`,`checkmail`,`mb`,`tjr`)
	   VALUES ('$pwd','10','0','$email',
	   '$jointime','$joinip','$jointime','$joinip','$checkmail','$mobile','$tjr'); ";
		if($dsql->ExecuteNoneQuery($inQuery))
		{
			$mid = $dsql->GetLastID();
			
		$mak=$dsql->getone("select mb_1 from #@__mb where aid='0'");
		$mak1=$dsql->getone("select mb_1 from #@__mb where aid='1'");
		if($mak['mb_1']=='1')
		{ // 发送短信
		$omb=okmb($mobile,$passwd);
		}
		
		if($mak1['mb_1']=='1')
		{ // 发送邮件
		$omb1=sendreg($email,$passwd,$mobile,'');
		}

			//----------------------------------------------
			//模拟登录
			//---------------------------
			$cfg_ml = new MemberLogin(7*3600);
			$rs = $cfg_ml->CheckUser($mobile, $passwd);

			
		//邮件验证
		if($cfg_mb_spacesta==-10)
		{
		$userhash = md5($cfg_cookie_encode.'--'.$mid.'--'.$email);
	  	$url = $cfg_basehost.(empty($cfg_cmspath) ? '/' : $cfg_cmspath)."/member/index_do.php?fmdo=checkMail&mid={$mid}&userhash={$userhash}&do=1";
	  	$url = eregi_replace('http://', '', $url);
	  	$url = 'http://'.eregi_replace('//', '/', $url);
	  	$mailtitle = "{$cfg_webname}--会员邮件验证通知";
	  	$mailbody = '';
	  	$mailbody .= "尊敬的用户[{$uname}]，您好：\r\n";
	  	$mailbody .= "欢迎注册成为[{$cfg_webname}]的会员。\r\n";
	  	$mailbody .= "要通过注册，还必须进行最后一步操作，请点击或复制下面链接到地址栏访问这地址：\r\n\r\n";
	  	$mailbody .= "{$url}\r\n\r\n";
	  	$mailbody .= "Power by http://www.dedecms.com 织梦内容管理系统！\r\n";
	  
			$headers = "From: ".$cfg_adminemail."\r\nReply-To: ".$cfg_adminemail;
			if($cfg_sendmail_bysmtp == 'Y' && !empty($cfg_smtp_server))
			{		
				$mailtype = 'TXT';
				require_once(DEDEINC.'/mail.class.php');
				$smtp = new smtp($cfg_smtp_server,$cfg_smtp_port,true,$cfg_smtp_usermail,$cfg_smtp_password);
				$smtp->debug = false;
				$smtp->sendmail($email,$cfg_webname,$cfg_smtp_usermail, $mailtitle, $mailbody, $mailtype);
			}
			else
			{
				@mail($email, $mailtitle, $mailbody, $headers);
			}
		}//End 邮件验证
			
		ShowMsg("恭喜您，注册成功！现在转向到会员中心", "/users/");
		exit();
			
		}
		else
		{
			ShowMsg("注册失败，请检查资料是否有误或与管理员联系！", "-1");
			exit();
		}
	}
	
	require_once(DEDEMEMBER."/templets/reg-new.htm");
	
}else {
	if(!$cfg_ml->IsLogin())
	{
		ShowMsg("尚未完成基本信息的注册,请返回重新填写！", "index_do.php?fmdo=user&dopost=regnew");
		exit;
	} else {
			if($cfg_ml->fields['spacesta'] == 2)
			{
				 ShowMsg('你已经登陆系统，无需重新注册！', 'index.php');
				 exit;
			}
	}
	$membermodel = new membermodel($cfg_ml->M_MbType);
	$postform = $membermodel->getForm(true);


	require_once(DEDEMEMBER."/templets/reg-new2.htm");
}



?>